package com.ruoyi.durian.service.impl;

import com.alipay.api.response.AlipayTradePagePayResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.OrderType;
import com.ruoyi.common.utils.aliPay.MyALiPayUtils;
import com.ruoyi.durian.domain.LlOrder;
import com.ruoyi.durian.domain.LlOrderInfo;
import com.ruoyi.durian.domain.TPay;
import com.ruoyi.durian.mapper.LlOrderInfoMapper;
import com.ruoyi.durian.mapper.LlOrderMapper;
import com.ruoyi.durian.service.TPayService;
import com.ruoyi.durian.mapper.TPayMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @author Lenovo
 * @description 针对表【t_pay(支付交易表)】的数据库操作Service实现
 * @createDate 2024-08-05 19:54:47
 */
@Service
public class TPayServiceImpl extends ServiceImpl<TPayMapper, TPay>
        implements TPayService{

    @Resource
    private MyALiPayUtils myALiPayUtils;

    @Resource
    private LlOrderInfoMapper llOrderInfoMapper;

    @Resource
    private TPayMapper tPayMapper;

    @Resource
    private LlOrderMapper llOrderMapper;
    /**
     * 支付宝支付
     * @param orderNo
     * @return
     * @throws java.io.IOException
     */
    @Override
    public String submitAlipay(String orderNo) {
        long aLong = Long.parseLong(orderNo);
        LambdaQueryWrapper<LlOrderInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LlOrderInfo::getOrderNo, aLong)
                .eq(LlOrderInfo::getIsDelete, 0);
        LlOrderInfo llOrderInfo = llOrderInfoMapper.selectOne(wrapper);
        saveTPay(llOrderInfo);
        try {
            AlipayTradePagePayResponse pay = myALiPayUtils.pay(llOrderInfo.getOrderNo().toString(), llOrderInfo.getAllPrice(),llOrderInfo.getOrderNo().toString());
            if (pay.isSuccess()){
                return pay.getBody();
            }
            return "支付失败";
        } catch (Exception e) {
            e.printStackTrace();
            return "服务器正在升级哦...";
        }
    }
    /**
     * 支付宝回调
     * @param paramsMap
     * @return
     */
    @Override
    public AjaxResult callback(Map<String, String> paramsMap) {

        if (paramsMap.get("trade_status").equals("TRADE_SUCCESS")){
            String outTradeNo = paramsMap.get("out_trade_no");
            LambdaQueryWrapper<TPay> eq = new LambdaQueryWrapper<TPay>().eq(TPay::getOrderNo, outTradeNo);
            TPay paymentInfo = getOne(eq);
            paymentInfo.setUpdateTime(new Date());
            paymentInfo.setOrderNo(outTradeNo);
            updateById(paymentInfo);
            Long payNo = paymentInfo.getPayNo();
            LambdaUpdateWrapper<LlOrder> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(LlOrder::getId, payNo)
                    .set(LlOrder::getStatus, OrderType.PAYMENT_COMPLETED);
            llOrderMapper.update(null, wrapper);
            return AjaxResult.success("操作成功");
        }
        return AjaxResult.error("操作失败");
    }

    public void saveTPay(LlOrderInfo llOrderInfo){
        TPay tPay1 = tPayMapper.selectOne(new LambdaQueryWrapper<TPay>().eq(TPay::getOrderNo, llOrderInfo.getOrderNo()));
        if (tPay1==null){
            TPay tPay = new TPay();
            tPay.setAmount(llOrderInfo.getAllPrice());
            tPay.setCreateTime(new Date());
            tPay.setPayNo(Long.valueOf(llOrderInfo.getOrderNo()));
            tPay.setOrderNo(llOrderInfo.getOrderNo().toString());
            tPay.setDeleted(0);
            tPay.setUserId(llOrderInfo.getUserId());
            tPay.setUpdateTime(new Date());
            tPayMapper.insert(tPay);
        }
    }
}




